
import Router from 'koa-router'
import {Models} from '../../../common/typings/model'
import svgCaptcha from 'svg-captcha'
import crypto from 'crypto'
import httpErrors from '../../../core/HttpException'
import axios from 'axios'

const router = new Router({
    prefix:'/api/v1/ar'
})

const config = {
    "serverHost":"http://839441c0d5079d54c1e3cdb2171a032b.cn1.crs.easyar.com:8888/",
    "apiKey": "c69eb0c22d8d42a6f276ba4e5bad2a9b",
    "apiSecret": "086112269581e3f62613fa5f627b9119636ec09077131eda0d0bbe4156145102"
}

function genSign(params: { [x: string]: string }, apiSecret: string) {
    var paramsStr = Object.keys(params).sort().map(function(key) {
        return key + params[key];
    }).join('') + apiSecret;
    return crypto.createHash('sha256').update(paramsStr).digest('hex');
}
/*
 * 获取验证码
 * @return { image } 返回图片
 */
router.get('/getTarget.do',async (ctx: Models.Ctx,next)=> {
    let params = ctx.request.query as { [x: string]: string }
    params.date = new Date().getTime().toString()
    let query = genSign(params,config.apiSecret)
    const res = await axios(`${config.serverHost}/target/8c7957ec-6e80-4cef-9f3e-ffa6e0b8e26f?${query}`).then(data=> {
        console.log(data)
        return data
    })
    throw new httpErrors.Success(res)
})

export default router